package com.hnhuas.salary.modules.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnhuas.salary.common.BaseController;
import com.hnhuas.salary.common.utils.R;
import com.hnhuas.salary.modules.admin.entity.SalaryNoticeUserEntity;
import com.hnhuas.salary.modules.admin.service.SalaryNoticeUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by weirdor on 2019-08-29 21:58
 */
@RestController
@RequestMapping("/api")
public class ApiNoticeController extends BaseController {

    @Autowired
    private SalaryNoticeUserService noticeUserService;


    /**
     * 签到
     */
    @PostMapping("/qian")
    public R qian(Long noticeId, HttpServletRequest request) {
        //判断当前用户是否签到
        QueryWrapper<SalaryNoticeUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", getLoginUserId(request));
        queryWrapper.eq("notice_id", noticeId);
        int count = noticeUserService.count(queryWrapper);
        if (count == 0) {
            SalaryNoticeUserEntity noticeUserEntity = new SalaryNoticeUserEntity();
            noticeUserEntity.setUserId(getLoginUserId(request));
            noticeUserEntity.setNoticeId(noticeId);
            boolean is_save = noticeUserService.save(noticeUserEntity);
            if (is_save) {
                return R.ok("保存成功");
            } else {
                return R.error("保存失败");
            }
        }else {
            return R.ok("您已签收");
        }
    }

}
